import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:x_project/components/load_state_widget.dart';

/// Created by XieXin on 2020/3/16.
/// 基类
abstract class XState<T extends StatefulWidget> extends State<T> {
  ///页面显示内容，默认正常
  LoadState viewType = LoadState.normal;

  ///是否显示加载中
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void setState(fn) {
    if (mounted) super.setState(fn);
  }

  @override
  void dispose() {
    _isLoading = false;
    super.dispose();
  }

  void showLoading() {
    setState(() {
      _isLoading = true;
    });
  }

  void hideLoading() {
    setState(() {
      _isLoading = false;
    });
  }

  ///body处理，根据viewType显示对应页面和加载中
  Widget body(
    Widget body, {
    VoidCallback? onPressed,
    String? hintText,
    String? loadText,
    ViewStyle style = ViewStyle.large,
    ScrollPhysics? physics,
    bool shrinkWrap = false,
  }) {
    switch (viewType) {
      case LoadState.noData: //暂无数据
        body = LoadStateWidget(
          type: LoadState.noData,
          onPressed: onPressed,
          hintText: hintText,
          style: style,
          physics: physics,
          shrinkWrap: shrinkWrap,
        );
        break;
      case LoadState.networkAnomaly: //网络异常
        body = LoadStateWidget(
          type: LoadState.networkAnomaly,
          onPressed: onPressed,
          hintText: hintText,
          style: style,
          physics: physics,
          shrinkWrap: shrinkWrap,
        );
        break;
      default: //默认
        break;
    }
    body = Stack(
      alignment: Alignment.center,
      children: [
        body,
        _loadingWidget(loadText),
      ],
    );
    return body;
  }

  ///body加载中处理
  Widget loadBody({
    required Widget body,
    String? loadText,
    VoidCallback? onPressed,
  }) {
    body = Stack(
      alignment: Alignment.center,
      children: [
        body,
        _loadingWidget(loadText),
      ],
    );
    return body;
  }

  Widget _loadingWidget(String? loadText) {
    return !_isLoading
        ? const SizedBox.shrink()
        : Container(
            width: 200.w,
            height: 200.w,
            decoration: BoxDecoration(
              color: Colors.black12,
              borderRadius: BorderRadius.circular(16.w),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SpinKitThreeBounce(
                  color: Theme.of(context).primaryColor,
                ),
                loadText == null
                    ? const SizedBox.shrink()
                    : Text(
                        loadText,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(28),
                          color: Colors.white,
                        ),
                      ),
              ],
            ),
          );
  }

  ///获取空页面
  Widget? getEmptyViewWidget({
    VoidCallback? onPressed,
    String? hintText,
    ViewStyle style = ViewStyle.large,
  }) {
    switch (viewType) {
      case LoadState.noData: //暂无数据
        return LoadStateWidget(
          type: LoadState.noData,
          onPressed: onPressed,
          hintText: hintText,
          style: style,
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
        );
      case LoadState.networkAnomaly: //网络异常
        return LoadStateWidget(
          type: LoadState.networkAnomaly,
          onPressed: onPressed,
          hintText: hintText,
          style: style,
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
        );
      default: //默认
        return null;
    }
  }

  ///设置暂无数据
  void normalViewType() {
    viewType = LoadState.normal;
  }

  ///设置暂无数据
  void setNoData(bool isNoData) {
    setState(() {
      viewType = isNoData ? LoadState.noData : LoadState.normal;
    });
  }

  ///设置网络异常
  void setNetworkAnomaly(bool isNetworkAnomaly) {
    setState(() {
      viewType = isNetworkAnomaly ? LoadState.networkAnomaly : LoadState.normal;
    });
  }
}
